const hpyModel = require('../models/homephys.js')

class HomephyCtrl {

    async list() {
        let lst = await hpyModel.find();
        return lst.map(m=>m.toJSON());
    }

    async create(cmptData) {
        let cmpt = new hpyModel(cmptData);
        const a = await cmpt.save();
        return a.toJSON();
    }

    async retrieve(id) {
        let cmpt = await hpyModel.findById(id);
        return cmpt?cmpt.toJSON(): null;
    }

    async upsert(id, cmptData) {
        cmptData.id = id;
        let cmpt = await hpyModel.upsert(id, cmptData);
        return cmpt.toJSON();
    }
    async removeById(id) {
        await hpyModel.removeById(id);
        return 1;
    }
    async find(filter)
    {
        let r = await hpyModel.find(filter);
        return r.map(e=>e.toJSON());
    }
}

module.exports = new HomephyCtrl();